package com.colorfruit.admin.control.config;

import com.colorfruit.admin.control.intercepter.Auth4AdminInterceptor;
import com.colorfruit.admin.control.intercepter.AuthInterceptor;
import com.colorfruit.framework.validate.RequestBodyValidResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

/**
 * spring mvc 配置
 */
@Configuration
public class CustomMVCConfig extends WebMvcConfigurationSupport {

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new RequestBodyValidResolver());//用于参数校验
        super.addArgumentResolvers(resolvers);
    }

    ///////////////////////////
    //////过滤器配置
    @Bean
    AuthInterceptor authInterceptor() {
        return new AuthInterceptor();
    }

    @Bean
    Auth4AdminInterceptor auth4AdminInterceptor(){
        return new Auth4AdminInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // addPathPatterns 用于添加拦截规则
        // excludePathPatterns 用户排除拦截
        // 映射为 user 的控制器下的所有映射
       //registry.addInterceptor(authInterceptor()).addPathPatterns("/**");
       registry.addInterceptor(auth4AdminInterceptor()).addPathPatterns("/**");
    }

    ////////////////////////////
    //////跨域配置
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**").allowedOrigins("*").allowedHeaders("*").allowedMethods("*");
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
